# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : sample.py
# Date      : 2019/12/9 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------

import maya.cmds as mc
import maya.mel as mel

import configs.constants.suffix_constant as sc
import configs.constants.nodeType_constant as nc
import scripts.commonCmds as con
import scripts.curveCmds as crc


def selectTo(lname,mirror=True,):
    sel = mc.ls(sl=True)
    if not sel: return
    ctr_list = list()
    clu = mc.cluster(sel)[1]
    x, y, z = mc.xform(clu, q=True, rp=True)
    ctr = crc.createControl(con.lname(lname,sc.sCtr))
    ctr_list.append(ctr)
    mc.setAttr(ctr+".t",x,y,z)
    if mirror:
        dup = mc.duplicate(ctr)[0]
        nn = mc.rename(dup,ctr.replace("L_","R_"))
        ctr_list.append(nn)
        con.localMirror(nn,space="world")
    mc.delete(clu)
    for _ctr in ctr_list:
        jot = mc.createNode(nc.tJoint,name=_ctr.replace(sc.sCtr,sc.sJot),parent=_ctr)
        con.emptyGroup(_ctr)
        con.lockHideAttr(_ctr,["v"])
    return

def selectJointChain(sel_list,lname,mirror=True,colorId=13):
    if not sel_list: return
    pat = None
    jotlist = []
    for i,sl in enumerate(sel_list,1):
        ctr = crc.createControl(con.lname(lname,i,sc.sCtr),colorId=colorId)
        sec_ctr = mc.createNode(nc.tTransform,name=con.lname(lname,"sec",i,sc.sCtr),parent=ctr)
        sec_ctrShape = crc.addShape(sec_ctr)
        crc.scaleXYZ(sec_ctr,0.7)
        sec_ctr_grp = con.emptyGroup(sec_ctr,["Zero"])[0]
        jot = mc.createNode(nc.tJoint,name = con.lname(lname,i,sc.sJot),parent=sec_ctr)
        mc.delete(mc.parentConstraint(sl,ctr,mo=False))
        
        con.lockHideAttr(ctr,["v"])
        if pat : mc.parent(ctr,pat)
        else: mc.addAttr(ctr,ln="sec_vis",at="bool",dv=0,k=True)
        
        mc.connectAttr(con.lAttr(con.lname(lname,1,sc.sCtr),"sec_vis"),sec_ctrShape+".v",f=1)
        pat = ctr
        con.emptyGroup(ctr)
        jotlist.append(jot)
    if mirror:
        on = con.lname(lname,1,sc.sCtr,"Zero")
        dup = mc.duplicate(on)[0]
        con.localMirror(dup,"world")
        con.searchReplaceNames("L_", "R_", "hierarchy")
        mc.delete(on)
        rname = mc.rename(on.replace("L_","R_")+"1",on.replace("L_","R_"))
    return jotlist

if __name__ == '__main__':
    pass